package com.twu.biblioteca;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Library {


    private HashMap<Integer, Movie> mapOfMovies;
    private HashMap<Integer, Book> mapOfBooks;
    private HashMap<String, User> mapOfUsers;
    HashMap<Integer, Method> commandsForLoggedIn = new HashMap<Integer, Method>();
    HashMap<Integer, Method> commandsForNotLoggedIn = new HashMap<Integer, Method>();
    private User loggedInUser = null;

    public Library(HashMap<Integer, Movie> mapOfMovies, HashMap<Integer,Book> mapOfBooks, HashMap<String, User> mapOfUsers) throws Exception{
        this.mapOfMovies = mapOfMovies;
        this.mapOfBooks = mapOfBooks;
        this.mapOfUsers = mapOfUsers;

        commandsForLoggedIn.put(1, Library.class.getDeclaredMethod("getListOfAvailableMovies"));
        commandsForLoggedIn.put(2, Library.class.getDeclaredMethod("getListOfAvailableBooks"));
        commandsForLoggedIn.put(3, Library.class.getDeclaredMethod("checkOutMovie"));
        commandsForLoggedIn.put(4, Library.class.getDeclaredMethod("returnMovie"));
        commandsForLoggedIn.put(5, Library.class.getDeclaredMethod("checkOutBook"));
        commandsForLoggedIn.put(6, Library.class.getDeclaredMethod("returnBook"));
        commandsForLoggedIn.put(7, Library.class.getDeclaredMethod("viewProfile"));
        commandsForLoggedIn.put(8, Library.class.getDeclaredMethod("closeLibrary"));

        commandsForNotLoggedIn.put(1, Library.class.getDeclaredMethod("logInUser"));
        commandsForNotLoggedIn.put(2, Library.class.getDeclaredMethod("closeLibrary"));
    }


    public void getGreetingMessage(){
        System.out.println("Welcome to Biblioteca");
    }

    public void getMenu(){
        if(loggedInUser != null){
            System.out.println("1. List all available movies");
            System.out.println("2. List all available books");
            System.out.println("3. Checkout movie");
            System.out.println("4. Return movie");
            System.out.println("5. Checkout book");
            System.out.println("6. Return book");
            System.out.println("7. View Profile");
            System.out.println("8. Quit");
        }
        else{
            System.out.println("1. Log In");
            System.out.println("2. Exit");
        }

    }

    public void getListOfAvailableMovies(){
        System.out.println("All available movies");
        for(Map.Entry<Integer, Movie> entry : mapOfMovies.entrySet()){
            Movie movie = entry.getValue();
            if(movie.isAvailable()) System.out.println(movie);
        }
    }

    public void getListOfAvailableBooks(){
        System.out.println("All available books");

        for(Map.Entry<Integer, Book> entry : mapOfBooks.entrySet()){
            Book book = entry.getValue();
            if(book.isAvailable()) System.out.println(book);
        }

    }

    public void checkOutMovie(){
        int id = readInput();
        Movie movie = mapOfMovies.get(id);

        if(movie != null){
            if(movie.checkOutCopy()){
                System.out.println("Movie Checkout was Successful");
            }
            else
            {
                System.out.println("No copies of this movie is not available now");
            }
        }
        else
        {
            System.out.println("No such movie is found");
        }
    }

    public void checkOutBook(){
        int id = readInput();
        Book book = mapOfBooks.get(id);

        if(book != null){
            if(book.checkOutCopy()){
                System.out.println("Book Checkout was Successful");
            }
            else
            {
                System.out.println("No copies of this book is not available now");
            }
        }
        else
        {
            System.out.println("No such book is found");
        }
    }

    public void returnMovie(){

        int id = readInput();
        Movie movie = mapOfMovies.get(id);

        if(movie != null){
            if(movie.returnCopy()){
                System.out.println("Movie return was Successful");
            }
        }
        else
        {
            System.out.println("No such movie is found");
        }
    }

    public void returnBook(){
        int id = readInput();
        Book book = mapOfBooks.get(id);

        if(book != null){
            if(book.checkOutCopy()){
                System.out.println("Book return was Successful");
            }
        }
        else
        {
            System.out.println("No such book is found");
        }
    }

    public void logInUser(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter username and password");
        String userName = scanner.nextLine();
        String password = scanner.nextLine();

        User user = mapOfUsers.get(userName);
        if(user != null){
            if(user.login(userName, password)){
                loggedInUser = user;
                System.out.println("Logged in");
            }
            else{
                System.out.println("Invalid password");
            }
        }
        else {
            System.out.println("Invalid username");
        }
    }

    public void logOutUser(){
        loggedInUser = null;
        System.out.println("Logged out");
    }

    public HashMap<Integer, Method> getCommands(){
        if(loggedInUser!=null){
            return commandsForLoggedIn;
        }
        else{
            return commandsForNotLoggedIn;
        }
    }

    public void viewProfile(){
        System.out.println(loggedInUser);
    }

    public int readInput(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the id");
        int option = scanner.nextInt();
        scanner.nextLine();
        return option;
    }

    public void closeLibrary(){
        System.out.println("Thank you for using Biblioteca");
        System.exit(0);
    }

}
